package bjtu.cit.bookshop.service.impl;

import bjtu.cit.bookshop.daos.BookMapper;
import bjtu.cit.bookshop.entities.Book;
import bjtu.cit.bookshop.service.IBookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by jia19 on 2015/9/21.
 */
@Service("bookService")
public class BookServiceImpl implements IBookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public Book getBookById(int id) {
        return bookMapper.selectByPrimaryKey(id);
    }

    @Override
    public int createABook(Book book) {
        return this.bookMapper.insertSelective(book);
    }

    @Override
    public int deleteBookById(int id) {
        return this.bookMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateBookInfo(Book book) {
        return this.bookMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public List<Book> getBookByNotNullField(Book book) {
        return this.bookMapper.selectByNotNullFields(book);
    }

    @Override
    public List<Book> getBookList(int num, int offset) {
        List<Book> books = this.bookMapper.selectBookList();
        int endIndex = offset + num;
        if (offset >= books.size()) {
            return null;
        }
        if (endIndex >= books.size()) {
            endIndex = books.size();
        }
        return books.subList(offset, endIndex);
    }

    @Override
    public int getBookListLength() {
        return this.bookMapper.selectBookList().size();
    }

    @Override
    public List<Book> search(Book book) {
        return this.bookMapper.searchBook(book);
    }


}
